package com.monitor.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.monitor.service.IndexService;



public class IndexController extends Controller{
	private static final Logger log = Log4jLogger.getLogger(IndexController.class);
	private IndexService service = new IndexService();
	@ActionKey("/")
	public void index(){
		log.debug("进入index控制器");
		setAttr("averageHumidity", service.getAverageHumidity());
		setAttr("averageTemperature", service.getAverageTemperature());
		setAttr("sensors", service.getSensorRecord());
		setAttr("dayRecord", service.getDayReord());
		setAttr("monthRecord", service.getMonthRecord());
		setAttr("logs", service.getLogs());
		setAttr("logNum", service.getTodayLog());
		render("/WEB-INF/index.html");
		log.debug("结束index控制器");
	}
	
	@ActionKey("/api")
	public void api(){
		log.debug("进入api控制器");
		JSONObject json = new JSONObject();
		json.put("averageHumidity", service.getAverageHumidity());
		json.put("averageTemperature", service.getAverageTemperature());
		json.put("sensors", service.getSensorRecord());
		renderJson(json.toJSONString());
		log.debug("结束api控制器");
	}
	@ActionKey("/api/day")
	public void day(){
		JSONObject json = new JSONObject();
		json.put("dayRecord", service.getDayReord());
		renderJson(json.toJSONString());
	}
	@ActionKey("/api/set")
	public void set(){
		JSONObject json = new JSONObject();
		json.put("msg", "success");
		renderJson(json.toJSONString());
	}
}
